﻿#include "GUIGeometryMergerPlaneDialog.h"
#include "ui_GUIGeometryMergerPlaneDialog.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsGeometryData.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKGeoInterfaceFactory.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoModelOperLine.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeomTools.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoModelPoint.h"
#include "PickDataProvider/GUIPickInfo.h"
#include "PickDataProvider/PickedData.h"
#include "FITK_Kernel/FITKCore/FITKActionOperator.h"
#include "OperatorsInterface/GraphEventOperator.h"

#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoModelSurface.h"
#include "PickDataProvider/PickedData.h"
#include "PickDataProvider/PickedDataProvider.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoModelOperFace.h"

namespace GUI {

    GUIGeometryMergerPlaneDialog::GUIGeometryMergerPlaneDialog(Core::FITKActionOperator* oper, QWidget* parent)
        : GUIGeometryBaseDialog(nullptr, oper, parent)
    {
        _ui = new Ui::GUIGeometryMergerPlaneDialog();
        _ui->setupUi(this);
        this->createGeometryObj(Interface::FITKGeoEnum::FITKGeometryComType::FGTFillGapsFace);
        // 设置对话框关闭时自动删除
        setAttribute(Qt::WA_DeleteOnClose);
        this->init();
    }

    GUIGeometryMergerPlaneDialog::~GUIGeometryMergerPlaneDialog()
    {
        if (_ui) {
            delete _ui;
            _ui = nullptr;
        }
    }

    void GUIGeometryMergerPlaneDialog::init()
    {
        setWindowTitle(tr("FillFace"));
        Acoustics::FITKAcousticsGeometryData* geometryData = FITKAPP->getGlobalData()->getGeometryData<Acoustics::FITKAcousticsGeometryData>();
        if (!geometryData) return;

        // 获取默认名称
        QString name = geometryData->checkName(tr("FillFace-1"));
        _ui->lineEdit_Name->setText(name);
    }

    QString GUIGeometryMergerPlaneDialog::getNameText()
    {
        return _ui->lineEdit_Name->text();
    }

    void GUIGeometryMergerPlaneDialog::pickFinishedOper()
    {
        //获取拾取数据
        GraphData::PickedDataProvider* provider = GraphData::PickedDataProvider::getInstance();
        if (provider == nullptr)return;
        disconnect(provider, SIGNAL(sig_pickedOver()), this, SLOT(pickFinishedOper()));
        // 处理拾取数据
        QList<GraphData::PickedData*> pickList = provider->getPickedList();
        if (pickList.size() == 1) {
            GraphData::PickedData* pickData = pickList.at(0);
            if (pickData == nullptr) return;
            // 获取虚拓扑形状
            QList<int> virtualShapesID = pickData->getPickedIds();
            QHash<int, int> pickOther = pickData->getPickedOtherIDs();
            if (virtualShapesID.size() == 0 || !pickOther.contains(virtualShapesID.at(0))) return;
            if (_flag == 1)
            {
                _virtualTarget.VirtualTopoId = virtualShapesID.at(0);
                _virtualTarget.VirtualTopoIndex = pickOther.value(virtualShapesID.at(0));
                _virtualTarget.CmdId = pickData->getPickedDataObjId();
                _virtualTarget.Type = Interface::FITKGeoEnum::VTopoShapeType::VSFace;
            }
            else if (_flag == 2)
            {
                _virtualTool.VirtualTopoId = virtualShapesID.at(0);
                _virtualTool.VirtualTopoIndex = pickOther.value(virtualShapesID.at(0));
                _virtualTool.CmdId = pickData->getPickedDataObjId();
                _virtualTool.Type = Interface::FITKGeoEnum::VTopoShapeType::VSFace;
            }
        }
        _ui->label_Curve1->setText(_virtualTarget.isNull() ? tr("Picked (0)") : tr("Picked (1)"));
        _ui->label_Curve2->setText(_virtualTool.isNull() ? tr("Picked (0)") : tr("Picked (1)"));
    }

    void GUIGeometryMergerPlaneDialog::getDataFormWidget()
    {        
        Interface::FITKAbsGeoModelOperFace * fillFace = dynamic_cast<Interface::FITKAbsGeoModelOperFace*>(_obj);
        fillFace->setType(Interface::FITKAbsGeoModelOperFace::GeoFaceOperType::GBTFillGaps);
        fillFace->add(_virtualTarget);
        fillFace->add(_virtualTool);
    }

    void GUIGeometryMergerPlaneDialog::on_pushButton_Pick_clicked()
    {
        //拾取对象 几何面
        //拾取方式 单独数据拾取方式。（点选+追加拾取）    
        _flag = 1;
        enablePickSig(GUI::GUIPickInfo::POBJGeometryFace, GUI::GUIPickInfo::PickMethod::PMSingle);
    }

    void GUIGeometryMergerPlaneDialog::on_pushButton_Pick_2_clicked()
    {
        //拾取对象 几何面
        //拾取方式 单独数据拾取方式。（点选+追加拾取）    
        _flag = 2;
        enablePickSig(GUI::GUIPickInfo::POBJGeometryFace, GUI::GUIPickInfo::PickMethod::PMSingle);
    }

    void GUIGeometryMergerPlaneDialog::on_pushButton_OK_clicked()
    {
        if (_virtualTarget.isNull() == true || _virtualTool.isNull() == true) return;
        if (_isCreate)
            if (!this->createObjData()) return;
        else
            if (!this->editObjData()) return;

        if (_oper && _obj) {
            _oper->setArgs("objID", _obj->getDataObjectID());
        }
        this->accept();
    }

    void GUIGeometryMergerPlaneDialog::on_pushButton_Cancel_clicked()
    {
        this->close();
    }
}